﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using ZG_member.BLL;
using ZG_member.Model;

namespace ZG_member
{
    public partial class TaoCanfangan2 : Form
    {
        int kapianid = 0;
        public TaoCanfangan2(int id)
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            kapianid = id;
        }
        BLL.goodsType gt = new BLL.goodsType();
        BLL.consumeSolution cs = new BLL.consumeSolution();
        BLL.goods goodbll = new BLL.goods();
        private void TaoCanfangan2_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            List<Model.goodsType> gg = gt.GetModelList("");

            Model.consumeSolution csmodle = cs.GetModel(kapianid);
          
            if (gg.Count > 0)
            {
                this.cbogoodtype.DataSource = gg;
                this.cbogoodtype.DisplayMember = "typeName";
                this.cbogoodtype.ValueMember = "id";
                this.cbogoodtype.SelectedIndex = 0;
                jilian();
            }
            if (csmodle == null)
            {
                //MessageBox.Show("请您先添加消费方案！");
                //this.Close();
                return;
            }
            if (csmodle.Taocanxiangqing != "" && csmodle.Taocanxiangqing != null)
            {
                string[] liebiao = csmodle.Taocanxiangqing.Split('|');
                for (int i = 0; i < liebiao.Length; i++)
                {
                    if (liebiao[i] == "")
                    {
                        return;
                    }
                    string[] liebiaoxiangqing = liebiao[i].Split(',');
                    Model.goods goodmodle = goodbll.getModelGoodsIdString(liebiaoxiangqing[0]);
                    if (goodmodle == null)
                    {
                        continue;
                    }
                    string[] jiazailiebiao = { liebiaoxiangqing[0], goodmodle.goodsName, liebiaoxiangqing[1] };
                    this.dataGridView1.Rows.Add(jiazailiebiao);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void timer2_Tick(object sender, EventArgs e)
        {

        }
        int flag = 0;
        private void TaoCanfangan2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }
        BLL.goods g = new BLL.goods();

        public void jilian()
        {
            try
            {
                List<Model.goods> gg = g.GetModelList("goodstypeid = " + this.cbogoodtype.SelectedValue);
                if (gg.Count <= 0)
                {
                    MessageBox.Show("没有该分类的商品！");
                    return;
                }
                this.cbogoodname.DataSource = gg;
            }
            catch (Exception)
            {

                return;
            }
            this.cbogoodname.DisplayMember = "goodsName";
            this.cbogoodname.ValueMember = "goodsid";
            this.cbogoodname.SelectedIndex = 0;
        }

        private void cbogoodtype_SelectedIndexChanged(object sender, EventArgs e)
        {

            jilian();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.numericUpDown1.Value <= 0)
            {
                MessageBox.Show("请输入数量！");
                return;
            }
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells["goodname"].Value.ToString() == this.cbogoodname.Text)
                {
                    dataGridView1.Rows[i].Cells["goodcount"].Value = int.Parse(dataGridView1.Rows[i].Cells["goodcount"].Value.ToString()) + int.Parse(this.numericUpDown1.Value.ToString());
                    return;
                }
            }
            string[] tiaojian = { this.cbogoodname.SelectedValue.ToString(), this.cbogoodname.Text, this.numericUpDown1.Value.ToString() };
            this.dataGridView1.Rows.Add(tiaojian);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
        }



        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string taocanxiangqing = "";
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                taocanxiangqing += dataGridView1.Rows[i].Cells["goodsid"].Value.ToString() + "," + dataGridView1.Rows[i].Cells["goodcount"].Value.ToString() + "|";
            }
            if (kapianid.ToString() == "0")
            {
                ConsumeSolutionSettingForm.taocanxiangqing = taocanxiangqing;
                ConsumeSolutionSettingForm.taocanxiangqingMoney = this.txtMoney.Text == "" ? "0" : this.txtMoney.Text;
                this.Close();
                return;
            }


            if (cs.Updatetaocan(taocanxiangqing, kapianid.ToString(),Convert.ToDouble(this.txtMoney.Text)))
            {
                MessageBox.Show("保存成功！");
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败！");
            }

        }

        private void cbogoodname_TextUpdate(object sender, EventArgs e)
        {
            string goodsName = this.cbogoodname.Text;
            BLL.goods goods = new BLL.goods();
            DataTable dt = goods.GetList("goodsName like '%" + goodsName + "%'").Tables[0];
            cbogoodname.DataSource = dt;
            cbogoodname.DisplayMember = "goodsName";
            cbogoodname.ValueMember = "goodsId";
            this.cbogoodname.Text = goodsName;
            cbogoodname.DroppedDown = true;
            Cursor = Cursors.Default;
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick_1(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }

        private void dataGridView1_CellMouseDoubleClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
        }
    }
}
